package com.yimixian.app.ui.pull;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.text.TextPaint;
import android.util.Log;

import com.yimixian.app.R;
import com.ymx.sdk.util.DensityUtils;

/**
 * 下拉刷新一米动画
 */
class YimiPullDrawable extends RefreshDrawable implements Runnable {

    private Handler mHandler = new Handler();
    private boolean isRunning;
    private int mTop;
    private int mDrawWidth;
    private int mDrawHeight;
    private Rect mBounds;
    private float textSize = 16;
    private int index = 0;
    private TextPaint textPaint;
    private int pullStatus = 0;//0下拉状态不到临界点  1到达临界点松开可刷新 2松开即刷新中
    private int[] draws = new int[] {
            R.drawable.dropdown_loading_00, R.drawable.dropdown_loading_01,
            R.drawable.dropdown_loading_02, R.drawable.dropdown_loading_03,
            R.drawable.dropdown_loading_04, R.drawable.dropdown_loading_05,
            R.drawable.dropdown_loading_06, R.drawable.dropdown_loading_07
    };
    public YimiPullDrawable(Context context, PullRefreshLayout layout) {
        super(context, layout);
        Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(),
                draws[0]);
        mDrawWidth = bitmap.getWidth();
        mDrawHeight = bitmap.getHeight();
        bitmap.recycle();
        textSize = DensityUtils.sp2px(getContext(), textSize);
    }

    @Override
    public void start() {
        isRunning = true;
        mHandler.postDelayed(this, 10);
    }

    @Override
    public void stop() {
        isRunning = false;
        mHandler.removeCallbacks(this);
    }

    @Override
    public boolean isRunning() {
        return isRunning;
    }

    @Override
    public void setColorSchemeColors(int[] colorSchemeColors) {
    }

    @Override
    public void setPercent(float percent) {
        pullStatus = 0;
        if (percent == 1){
            pullStatus = 1;
        }
        if (percent == 2){//松开刷新
            pullStatus = 2;
        }
    }

    @Override
    public void offsetTopAndBottom(int offset) {
        mTop += offset;
        invalidateSelf();
    }

    @Override
    public void run() {
        if (isRunning) {
            mHandler.postDelayed(this, 50);
            invalidateSelf();
        }
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        mTop = -mDrawHeight - (getRefreshLayout().getFinalOffset() - mDrawHeight) / 2;
        mBounds = bounds;
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.translate(mBounds.width() / 2 - mDrawWidth, mTop);
        if (textPaint == null) {
            textPaint = new TextPaint();
            textPaint.setColor(getContext().getResources().getColor(R.color.app_black));
            textPaint.setTextSize(textSize);
            textPaint.setAntiAlias(true);
        }
        if (isRunning){
            Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(),
                    draws[index % draws.length]);
            canvas.drawBitmap(bitmap, 0, 0, new Paint());
            canvas.translate(mDrawWidth, mDrawHeight / 2 + textSize / 2);
            canvas.drawText("正在加载...", 0, 0, textPaint);
            bitmap.recycle();
        }else{
            Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(),
                    R.drawable.dropdown_anim_00);
            canvas.drawBitmap(bitmap, 0, 0, new Paint());
            canvas.translate(mDrawWidth, mDrawHeight / 2 + textSize / 2);
            if (pullStatus == 1){//
                canvas.drawText("松开刷新", 0, 0, textPaint);
            }else if (pullStatus == 2){
                canvas.drawText("正在加载...", 0, 0, textPaint);
            }else{
                canvas.drawText("下拉刷新", 0, 0, textPaint);
            }
            bitmap.recycle();
        }
        index++;
    }
}
